What happends if you enter text into the lower TextField and hit enter?

Answer:

The user can enter text into the lower box, and edit it using the arrow keys and other keys of the keyboard. But when the user hits enter, nothing happens.

The setEditable() Method

GUI

Nothing happens because the lower text field does not have a registered listener. You could modify the program so it has a listener, but this would not follow the original design for the program. A better idea (for this program) is to prevent the user from entering text into the wrong text field. This can be done with the setEditable() method:

text.setEditable( false );

Here text is a variable that refers to a JTextField. The setEditable() method has one parameter that evaluates to either true or false. If the parameter is false, then the field can not be altered by the user (the setText() method still works, however). Here is an excerpt from the program:

public class Repeater extends JFrame implements ActionListener
{

   JLabel inLabel    = new JLabel( "Enter Your Name:  " ) ;
   TextField inText  = new TextField( 15 );

   JLabel outLabel   = new JLabel( "Here is Your Name :" ) ;
   TextField outText = new TextField( 15 );
   
   public Repeater()      // constructor
   {  
      getContentPane().setLayout( new FlowLayout() ); 
      getContentPane().add( inLabel  ) ;
      getContentPane().add( inText   ) ;
      getContentPane().add( outLabel ) ;
      getContentPane().add( outText  ) ;

      inText.addActionListener( this );
      setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE );   
   }

  // The application code.
  void copyText()
  {
    String name = inText.getText();
    outText.setText( name );
  }

  . . . . . .

QUESTION 13:

Suggest a place to use the setEditable() method.